package com.example.rpcdemo;

import com.example.rpcdemo.rpc.client.RpcClient;
import com.example.rpcdemo.rpc.client.RpcProxy;
import com.example.rpcdemo.rpc.server.RpcServer;
import com.example.rpcdemo.service.HelloService;
import com.example.rpcdemo.service.impl.HelloServiceImpl;

public class RpcDemoTest {
    
    public static void main(String[] args) {
        try {
            // Start RPC Server in a separate thread
            new Thread(() -> {
                try {
                    RpcServer server = new RpcServer(8080);
                    server.addService("com.example.rpcdemo.service.HelloService", new HelloServiceImpl());
                    server.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
            
            // Wait for server to start
            Thread.sleep(2000);
            
            // Create RPC Client
            RpcClient client = new RpcClient("localhost", 8080);
            client.connect();
            
            // Create proxy for HelloService
            HelloService helloService = RpcProxy.createProxy(HelloService.class, client);
            
            // Test RPC call
            String result = helloService.sayHello("World");
            System.out.println("RPC Result: " + result);
            
            // Test multiple calls
            String result2 = helloService.sayHello("Alice");
            System.out.println("RPC Result 2: " + result2);
            
            // Close client
            client.close();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}